package com.zqj.lambda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * java内置的4大核心函数式接口
 *
 * 消费型接口 Consumer<T>     void accept(T t)
 * 共给型接口 Supplier<T>     T get()
 * 函数型接口 Function<T,R>   R apply(T 0)
 * 断定型接口 Predicate<T>    boolean test(T t)
 */
public class lambdaTest2 {

    @Test
    public void test1(){
        happyTime(700, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println("强哥，给你"+aDouble+"元你去天上人间消费吧");
            }
        });

        System.out.println("********************************");
        happyTime(800,money->  System.out.println("阳哥，给你"+money+"元你去天上人间消费吧"));
    }

    public void happyTime(double money, Consumer<Double> con){
        con.accept(money);
    }

    @Test
    public void test2(){
        List<String> list = Arrays.asList("北京","天津","南京","东京","普京");
        List<String> filterString = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(filterString);
        System.out.println("**********************************");
        List<String> list1 = filterString(list, s -> s.contains("京"));
        System.out.println(list1);
    }
    /**
     * 根据给顶的规则，过滤集合中的字符串。此规则由Predicate的方法决定
     * @param list
     * @param predicate
     * @return
     */
    public List<String> filterString(List<String> list, Predicate<String> predicate){
        ArrayList<String> filterList = new ArrayList<>();
        for (String s :list){
            if (predicate.test(s)){
                filterList.add(s);
            }
        }
        return filterList;

    }
}
